package cpr.context.ruleacceptor;

import java.util.Set;

import cpr.Rule;
import cpr.context.CacheRuleContext;

public class GlobalRuleNrSetRuleAcceptor implements IRuleAcceptor,IRuleAcceptorCache {
		Set ruleNrGlobalSet;
		int ruleNr;
		int ruleNrGlobal;
		CacheRuleContext cacheRuleContext;
		
		public GlobalRuleNrSetRuleAcceptor(Set ruleNrGlobalSet) {
			this.ruleNrGlobalSet = ruleNrGlobalSet;
			this.ruleNr = 0;
			this.ruleNrGlobal = 0;
		}
		public void setCacheRuleContext(CacheRuleContext cache) {
			cacheRuleContext = cache;
		}
		
		public int nextRuleGlobalNr() {
			int t = ruleNrGlobal;
			ruleNrGlobal ++;
			return t;
		}
		public boolean acceptGlobalNr(int globalNr) {
			if (ruleNrGlobalSet.contains(Integer.valueOf(globalNr))) {
				return true;
			} else {
				return false;
			}
		}
		
		public boolean accept(Rule aRule, int globalNr) {
			aRule.ruleNrGlobal = globalNr;
			if (ruleNrGlobalSet.contains(Integer.valueOf(aRule.ruleNrGlobal))) {
				aRule.ruleNr = ruleNr;
				ruleNr++;
				return true;
			} else {
				return false;
			}
		}
		
		
		
			public CacheRuleContext getCacheRuleContext() {
				return cacheRuleContext;
			}
			public boolean isCacheComplete() {
				if (cacheRuleContext==null) return false;
				return cacheRuleContext.containsAll(ruleNrGlobalSet);
			}
			public Set<Integer> getRequiredGlobalRuleNrs() {
				return ruleNrGlobalSet;
			}
		
}

